import { requestClient } from '#/api/request';

export namespace FinanceApi {
  /** 充值申请列表查询参数 */
  export interface RechargeListParams {
    page?: number;
    per_page?: number;
    status?: 'pending' | 'processing' | 'matched' | 'confirmed' | 'failed' | 'cancelled';
    search?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 充值申请记录接口 */
  export interface RechargeRecord {
    id: string;
    recharge_number: string;
    amount: number;
    status: 'pending' | 'processing' | 'matched' | 'confirmed' | 'failed' | 'cancelled';
    landing_company: string;
    tenant_id?: number | string;
    tenant_name?: string;
    tenant?: {
      id: number | string;
      name: string;
    };
    bank_name?: string;
    bank_account?: string;
    remarks?: string;
    created_at: string;
    updated_at: string;
  }

  /** 银行明细列表查询参数 */
  export interface BankDetailListParams {
    page?: number;
    per_page?: number;
    match_status?: '未匹配' | '已匹配' | '已忽略';
    cd_sign?: '0' | '1'; // 0: 收入, 1: 支出
    search?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 银行明细记录接口 */
  export interface BankDetailRecord {
    id: string;
    serial_no: string;
    transaction_datetime: string;
    amt: number;
    bal: number;
    cd_sign: '0' | '1';
    opp_acc_name?: string;
    opp_acc_no?: string;
    opp_acc_bank?: string;
    abs?: string;
    match_status: '未匹配' | '已匹配' | '已忽略';
    match_confidence?: number;
    recharge?: RechargeRecord;
    created_at: string;
    updated_at: string;
  }

  /** 分页响应结构 */
  export interface PaginatedResponse<T> {
    list: T[];
    pagination: {
      current_page: number;
      per_page: number;
      total: number;
      last_page: number;
      from?: number;
      to?: number;
    };
    statistics?: any;
  }

  /** 匹配建议 */
  export interface MatchSuggestion {
    recharge?: RechargeRecord;
    bank_detail?: BankDetailRecord;
    confidence: number;
    match_reasons: string[];
  }

  /** 匹配结果 */
  export interface MatchResult {
    matched_count: number;
    success_rate: number;
    details: Array<{
      recharge_id: number;
      bank_detail_id: number;
      confidence: number;
    }>;
  }
}

/**
 * 获取充值申请列表
 */
export async function getRechargeListApi(params: FinanceApi.RechargeListParams) {
  return requestClient.get<FinanceApi.PaginatedResponse<FinanceApi.RechargeRecord>>('/api/admin/recharges', {
    params,
  });
}

/**
 * 获取充值申请详情
 */
export async function getRechargeDetailApi(id: string) {
  return requestClient.get<{
    recharge: FinanceApi.RechargeRecord;
    match_suggestions: FinanceApi.MatchSuggestion[];
  }>(`/api/admin/recharges/${id}`);
}

/**
 * 执行匹配算法
 */
export async function runMatchingApi() {
  return requestClient.post<FinanceApi.MatchResult>('/api/admin/recharges/run-matching');
}

/**
 * 确认匹配
 */
export async function confirmMatchApi(rechargeId: string, bankDetailId?: string) {
  return requestClient.post<{ success: boolean; message: string }>(`/api/admin/recharges/${rechargeId}/confirm-match`, {
    bank_detail_id: bankDetailId,
  });
}

/**
 * 获取银行明细列表
 */
export async function getBankDetailListApi(params: FinanceApi.BankDetailListParams) {
  return requestClient.get<FinanceApi.PaginatedResponse<FinanceApi.BankDetailRecord>>('/api/admin/bank-details', {
    params,
  });
}

/**
 * 获取银行明细详情
 */
export async function getBankDetailApi(id: string) {
  return requestClient.get<{
    detail: FinanceApi.BankDetailRecord;
    match_suggestions: FinanceApi.MatchSuggestion[];
  }>(`/api/admin/bank-details/${id}`);
}

/**
 * 更新银行明细状态
 */
export async function updateBankDetailStatusApi(id: string, status: string, reason?: string) {
  return requestClient.put<{ success: boolean; message: string }>(`/api/admin/bank-details/${id}/status`, {
    status,
    reason,
  });
}
/**
 * 触发银行明细同步
 */
export async function syncBankDetailsApi() {
  return requestClient.post<{ code: number; message: string; data: null }>(`/api/admin/bank-details/sync`);
}
